package mx.com.crowdgine.assetloader.audio;

import java.io.File;
import java.io.IOException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.sampled.UnsupportedAudioFileException;


import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class MidiAudio implements Audio{
	
	private AudioStream sound;
	private AudioPlayer player;
	private Sequence sequence;
	private Sequencer sequencer;
	
	
	public void load(String filename){		
		try {
			sequencer = MidiSystem.getSequencer();
	        sequence = MidiSystem.getSequence(new File(filename));
	    } catch (IOException e) {
	    } catch (MidiUnavailableException e) {
	    } catch (InvalidMidiDataException e) {
	    }
	}
	
	public void play(){
		try {
			if(!sequencer.isOpen()&&!sequencer.isRunning()){
				sequencer = MidiSystem.getSequencer();
		        sequencer.open();
		        sequencer.setSequence(sequence);
				sequencer.start();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void stop() {
		if(sequencer.isRunning()){
			sequencer.stop();
		}
	}
}
